import { program } from 'commander'
import path from 'path'
import { generateFromEntity } from '../generators/fromEntity'

program
  .command('generate')
  .description('Generate CRUD files from Midway entity')
  .option('-e, --entity <path>', 'Entity file path')
  .option('-n, --name <string>', 'Module name (e.g. user, order)')
  .action(async (options) => {
    try {
      const entityPath = path.resolve(process.cwd(), options.entity)
      const name = options.name

      if (!entityPath) {
        console.error('❌ Please provide an entity file path with --entity')
        process.exit(1)
      }

      if (!name) {
        console.error('❌ Please provide a module name with --name')
        process.exit(1)
      }

      await generateFromEntity(entityPath, name)
      console.log('✅ CRUD generation complete!')
    } catch (error) {
      console.error('❌ CRUD generation failed:', error)
      process.exit(1)
    }
  })

if (require.main === module) {
  program.parse(process.argv)
}
